// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   GroupTxAction.java

package com.iphaeton.lms.action.organize;

import com.iphaeton.common.excel.IExcelService;
import com.iphaeton.common.helper.Pagination;
import com.iphaeton.common.helper.UserSession;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.lms.business.organize.*;
import com.iphaeton.lms.pojo.organize.*;
import java.io.IOException;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.log4j.Logger;
import org.apache.struts.action.*;
import org.dom4j.io.XMLWriter;

// Referenced classes of package com.iphaeton.lms.action.organize:
//            OrganizeBizSupportAction

public class GroupTxAction extends OrganizeBizSupportAction
{

    public GroupTxAction()
    {
    }

    public ActionForward editGroup(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String page = request.getParameter("page");
        String id = request.getParameter("id");
        Group g = null;
        try
        {
            g = group.findGroupById(id);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("currPage", page);
        request.setAttribute("type", (new StringBuffer(String.valueOf(g.getType()))).toString());
        request.setAttribute("group", g);
        return mapping.findForward("groupEdit");
    }

    public ActionForward addGroup(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        daf = (DynaActionForm)form;
        String name = (String)daf.get("name");
        String comment = (String)daf.get("comment");
        String page = (String)daf.get("page");
        String organizeId = (String)daf.get("organizeId");
        char type = ((String) (daf.get("type"))).charAt(0);
        Group g = new Group();
        try
        {
            g.setComment(comment);
            g.setName(name);
            if(type != ' ' && type != 'n')
                g.setType(type);
            Organize org = null;
            if(!"".equals(organizeId))
                org = organize.findOrganizeById(organizeId);
            else
                org = organize.findRoot();
            organize.addGroup(org, g);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("currPage", page);
        request.setAttribute("groupType", (new StringBuffer(String.valueOf(g.getType()))).toString());
        request.setAttribute("type", "create");
        request.setAttribute("group", g);
        return mapping.findForward("groupEditOk");
    }

    public ActionForward updateGroup(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        daf = (DynaActionForm)form;
        String id = (String)daf.get("id");
        String name = (String)daf.get("name");
        String comment = (String)daf.get("comment");
        String page = (String)daf.get("page");
        char type = ((String) (daf.get("type"))).charAt(0);
        Group g = null;
        try
        {
            g = group.findGroupById(id);
            g.setComment(comment);
            g.setName(name);
            if(type != ' ' && type != 'n')
                g.setType(type);
            g = group.updateGroup(g);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("currPage", page);
        request.setAttribute("groupType", (new StringBuffer(String.valueOf(g.getType()))).toString());
        request.setAttribute("type", "edit");
        request.setAttribute("group", g);
        return mapping.findForward("groupEditOk");
    }

    public ActionForward selectUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        daf = (DynaActionForm)form;
        int currentPage = 0;
        int pageSize = 5;
        String page = (String)daf.get("page");
        String id = request.getParameter("id");
        String row = request.getParameter("row");
        Group g = null;
        List user = null;
        try
        {
            g = group.findGroupById(id);
            if(page == null || page.trim().equals(""))
                currentPage = 1;
            else
            if(page != null && !page.trim().equals(""))
                currentPage = Integer.parseInt(page);
            pageInfo = group.selectUser(id, row, pageSize);
            pageInfo.setPage(currentPage);
            pageInfo.setRow(row);
            request.getSession().setAttribute("pageInfo", pageInfo);
            user = group.findPageList(pageInfo);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        if(user == null)
            user = new LinkedList();
        request.setAttribute("group", g);
        request.setAttribute("gUser", user);
        return mapping.findForward("group_user_list");
    }

    public ActionForward removeUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        daf = (DynaActionForm)form;
        int currentPage = 0;
        int pageSize = 5;
        String page = (String)daf.get("page");
        String id = request.getParameter("id");
        String userId[] = request.getParameter("user_id").split(";");
        Group g = null;
        Set newUser = new HashSet();
        List list = null;
        try
        {
            g = group.findGroupById(id);
            list = group.findGroupTrain(id, "force");
            for(int i = 0; i < userId.length; i++)
            {
                User u = user.findUserById(userId[i]);
                group.removeUser(g, userId[i]);
                newUser.add(u);
                for(int j = 0; j < list.size(); j++)
                {
                    String tid = (String)((Map)list.get(j)).get("tid");
                    if(organize.isChild(u.getOrganize(), train.findTrainById(tid).getOrganize()))
                        train.removeStudent(userId[i], tid);
                }

            }

            currentPage = 1;
            pageInfo = group.findUserPagination(id, pageSize);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        pageInfo.setPage(currentPage);
        request.getSession().setAttribute("pageInfo", pageInfo);
        request.setAttribute("page", page);
        request.setAttribute("type", "remove");
        request.setAttribute("group", g);
        request.setAttribute("gUser", newUser);
        return mapping.findForward("user_operate_ok");
    }

    public ActionForward getUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        daf = (DynaActionForm)form;
        String id = request.getParameter("id");
        String organize_id = request.getParameter("o_id");
        Group g = null;
        List user = new LinkedList();
        try
        {
            g = group.findGroupById(id);
            Organize org = g.getOrganize();
            user = group.findNotGroupUser(org.getUrl(), id, organize_id);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("group", g);
        request.setAttribute("user", user);
        return mapping.findForward("user_add");
    }

    public ActionForward addUser(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        String userId[] = request.getParameter("userId").split(";");
        Group g = null;
        List list = null;
        try
        {
            list = group.findGroupTrain(id, "force");
            g = group.findGroupById(id);
            for(int i = 0; i < userId.length; i++)
            {
                User u = user.findUserById(userId[i]);
                group.addUser(g, u);
                for(int j = 0; list != null && j < list.size(); j++)
                {
                    String tid = (String)((Map)list.get(j)).get("tid");
                    if(organize.isChild(u.getOrganize(), train.findTrainById(tid).getOrganize()))
                        train.assignStudent(tid, userId[i], 1);
                }

            }

        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("type", "add");
        request.setAttribute("group", g);
        request.setAttribute("gUser", g.getUser());
        return mapping.findForward("user_operate_ok");
    }

    public ActionForward getUserDocument(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String type = request.getParameter("type");
        String trainId = request.getParameter("trainId");
        org.dom4j.Document doc = null;
        try
        {
            Train t = train.findTrainById(trainId);
            doc = group.getUserDocument(type, t);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        response.setContentType("text/xml; charset=UTF-8");
        javax.servlet.ServletOutputStream sos = response.getOutputStream();
        XMLWriter writer = new XMLWriter(sos);
        writer.write(doc);
        writer.flush();
        writer.close();
        return null;
    }

    public ActionForward importGroupInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        daf = (DynaActionForm)form;
        String dataFile = (String)daf.get("name");
        String type = request.getParameter("type");
        List list = null;
        try
        {
            list = excel.readExcel(dataFile);
            for(int i = 0; i < list.size(); i++)
            {
                Group g = new Group();
                List temp = (List)list.get(i);
                g.setName(temp.get(0).toString().trim());
                g.setComment(temp.get(1) == null ? null : temp.get(1).toString().trim());
                if(type.equals("station"))
                    g.setType('g');
                else
                if(type.equals("duty"))
                    g.setType('z');
                g.setOrganizeId(organize.findRoot().getId());
                g = group.updateGroup(g);
            }

        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("type", type);
        return mapping.findForward("import_complete");
    }

    public ActionForward getGroupTree(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0L);
        String type = request.getParameter("type");
        String id = request.getParameter("id");
        if(id == null || "".equals(id))
            id = ((UserSession)request.getSession().getAttribute("userSession")).getManageOrgId();
        org.dom4j.Document doc = null;
        try
        {
            doc = group.getGroupTree(request.getContextPath(), id, type);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        response.setContentType("text/xml; charset=UTF-8");
        javax.servlet.ServletOutputStream sos = response.getOutputStream();
        XMLWriter writer = new XMLWriter(sos);
        writer.write(doc);
        writer.flush();
        writer.close();
        return null;
    }

    private static Logger logger;
    private Pagination pageInfo;
    private DynaActionForm daf;
    static Class class$0; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.action.organize.GroupTxAction.class.getName());
    }
}
